import axios from 'axios'
import { Toast } from 'vant'

const service = axios.create({
  baseURL: process.env.VUE_APP_API, // api的base_url
  timeout: 5000 // 请求超时时间
})

// request请求拦截器
service.interceptors.request.use(
  config => {
    return config
  },
  error => {
    // 处理发生请求错误的情况
    console.log(error) // for debug
    return Promise.reject(error)
  }
)

// response响应拦截器
service.interceptors.response.use(
  response => {
    const res = response.data
    if (res.code === 20000) {
      return res
    } else {
      Toast(res.errMsg)
      return Promise.reject(res.errMsg)
    }
  },
  error => {
    console.log(error)
    return Promise.reject(error)
  }
)

export default service
